package handler

import (
	"admin-ease/global"
	"admin-ease/internal/dto"
	"admin-ease/internal/model"
	"admin-ease/pkg"
	"encoding/json"
	"net/http"

	"github.com/gin-gonic/gin"
)

type CommentHandel struct {
}

func NewCommentHandel() *CommentHandel {
	return &CommentHandel{}
}
func (co *CommentHandel) CommentAdd(c *gin.Context) {
	var req dto.CommentReq
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, dto.UserResp{
			Msg:  "参数获取失败",
			Code: 500,
		})
		return
	}
	comment := model.Comment{
		UserId:  req.UserId,
		Content: req.Content,
	}
	err = global.DB.Debug().Create(&comment).Error
	if err != nil {
		c.JSON(http.StatusBadRequest, dto.UserResp{
			Msg:  "评论失败",
			Code: 500,
		})
		return
	}
	simple := pkg.NewRabbitMQSimple("Comment_message")
	m := map[string]interface{}{
		"UserId":  req.UserId,
		"Content": req.Content,
	}
	marshal, _ := json.Marshal(m)
	publishSimple := simple.PublishSimple(string(marshal))
	if publishSimple != true {
		return
	}
	c.JSON(http.StatusOK, dto.UserResp{
		Msg:  "评论成功",
		Code: 200,
		Data: comment.Content,
	})
	return
}
